<?php
class Page_AdminController extends Main_Controller_AdminAction
{
	/**
	 * @var string
	 */
	const PAGER_NAME_LAYOUT = 'Page_AdminLayoutPager';
	
	/**
	 * @var string
	 */
	const PAGER_NAME_PAGE = 'Page_AdminPagePager';
	
	/**
	 * @var string
	 */
	const PAGER_NAME_BLOCK = 'Page_AdminBlockPager';
	
	/**
	 * @var App_Page_Form_AdminLayoutEdit
	 */
	private $_formAdminLayoutEdit;
	
	/**
	 * @var App_Page_Form_AdminPageEdit
	 */
	private $_formAdminPageEdit;
	
	/**
	 * @var App_Page_Form_AdminBlockEdit
	 */
	private $_formAdminBlockEdit;
	
	/**
	 * @var App_Page_Service_Layout
	 */
	private $_serviceLayout;
	
	/**
	 * @var App_Page_Service_Page
	 */
	private $_servicePage;
	
	/**
	 * @var App_Admin_Service_Lang
	 */
	private $_serviceLang;
	
	/**
	 * @var App_Page_Service_Block
	 */
	private $_serviceBlock;
	
	/**
	 * @return void
	 */
	public function init() 
	{
		$this->_formAdminLayoutEdit = Main_Form::factory('Page_Form_AdminLayoutEdit');
		$this->_formAdminPageEdit = Main_Form::factory('Page_Form_AdminPageEdit');
		$this->_formAdminBlockEdit = Main_Form::factory('Page_Form_AdminBlockEdit');
		$this->_serviceLayout = new App_Page_Service_Layout();
		$this->_servicePage = new App_Page_Service_Page();
		$this->_serviceBlock = new App_Page_Service_Block();
		$this->_serviceLang = new App_Admin_Service_Lang();
		parent::init();
	}
	
	// --- layout --- {{ 
	
	/**
	 * @return void
	 */
	public function layoutListAction()
	{
		return $this->render();
	}
	
	/**
	 * @return void
	 */
	public function layoutAddAction() 
	{
		$form = $this->_formAdminLayoutEdit;
		
		if ($this->_request->isPost()) {
			$data = $this->_getAllParams();
            if ($form->isValid($data)) {
 		 		$this->_serviceLayout->insertItem($data);
 		 		return $this->_redirect('/page/admin/layout.list' . Main_Const::getSeriveUrlStr());
            }
		}    
		
		$this->view->form = $form;
		return $this->render();
	}
	
	/**
	 * @return void
	 */
	public function layoutEditAction() 
	{
		$layoutId = $this->_getParam('id');
		
		$form = $this->_formAdminLayoutEdit;
		
		if (! $this->_request->isPost()) {
			$dataDb = $this->_serviceLayout->getItemById($layoutId);
			$form->setFormValues($dataDb);
		} else {
			$data = $this->_getAllParams();
            if ($form->isValid($data)) {
 		 		$this->_serviceLayout->updateItem($data, $layoutId);
 		 		return $this->_redirect('/page/admin/layout.list' . Main_Const::getSeriveUrlStr());      		    			
            }
		}   
		
		$this->view->form = $form;
		return $this->render();
	}
	
	
	/**
	 * @return json
	 */
	public function ajaxPagerLayoutAction()
	{
	    if (!$this->_request->isXmlHttpRequest()) {
            $this->_response->setHttpResponseCode(404)->sendHeaders();
            return $this->_redirect('/error/error');
        }
        
     	$pager = Main_Pager::factory('Page_Pager_AdminLayout', self::PAGER_NAME_LAYOUT); 
     	
     	$result = $pager->getResponse($_REQUEST);

     	$this->_helper->json($result);   
	}
	
	// --- layout --- }}

	// --- page --- {{ 
	
	/**
	 * @return void
	 */
	public function pageListAction()
	{
		return $this->render();
	}
	
	/**
	 * @return void
	 */
	public function pageAddAction() 
	{
		$form = $this->_formAdminPageEdit;
		
		if ($this->_request->isPost()) {
			$data = $this->_getAllParams();
            if ($form->isValid($data)) {
				$this->_servicePage->insertItem($data);
 		 		return $this->_redirect('/page/admin/page.list' . Main_Const::getSeriveUrlStr());	            	
            }
		}    
		
		$this->view->form = $form;
		return $this->render();
	}
	
	/**
	 * @return void
	 */
	public function pageEditAction() 
	{
		$pageId = $this->_getParam('id');
		$form = $this->_formAdminPageEdit;
		$dataDb = $this->_servicePage->getItemById($pageId);
		if ($dataDb['level'] == 1) {
			$form->getElement('name')->setAttrib('disabled', true);
			$form->getElement('name')->setRequired(false);
			$form->getElement('parent_id')->setAttrib('disabled', true);
		}
		
		
		if (! $this->_request->isPost()) {
			$form->setFormValues($dataDb);
		} else {
			$data = $this->_getAllParams();
            if ($form->isValid($data)) {
 		 		$this->_servicePage->updateItem($data, $pageId);
 		 		return $this->_redirect('/page/admin/page.list' . Main_Const::getSeriveUrlStr());      		    			
            }
		}   
		
		$this->view->form = $form;
		return $this->render();
	}
	
	
	/**
	 * @return json
	 */
	public function ajaxPagerPageAction()
	{
	    if (!$this->_request->isXmlHttpRequest()) {
            $this->_response->setHttpResponseCode(404)->sendHeaders();
            return $this->_redirect('/error/error');
        }
        
     	$pager = Main_Pager::factory('Page_Pager_AdminPage', self::PAGER_NAME_PAGE); 
     	
     	$result = $pager->getResponse($_REQUEST);

     	$this->_helper->json($result);   
	}
	
	// --- page --- }}
	
	// --- block --- {{
	/**
	 * @return void
	 */
	public function blockListAction() 
	{
		$pageId = $this->_getParam('id');
		$pageItem = $this->_servicePage->getItemById($pageId);
		$this->view->ascendantBranch = $this->_servicePage->getAscendantBranchIdAndName($pageId);
		
		$tabId = $this->_getParam('tab_id',0);
		$this->view->tabId = $tabId;
		
		$this->view->pageId = $pageId;
		$this->view->pageItem = $pageItem;
		
		$langs = $this->_serviceLang->getAll(); 
		$this->view->langs = Main_Function::expandList($langs, 'id');

		$this->view->blockStatistics = $this->_servicePage->getBlockStatisticsInPage($pageId);
		
		return $this->render();
	}

	/**
	 * @return void
	 */
	public function blockEditAction() 
	{
		$blockId = $this->_getParam('id');
		$pageId = $this->_getParam('page_id');
		
		$form = $this->_formAdminBlockEdit;
		
		if (! $this->_request->isPost()) {
			$dataDb = $this->_serviceBlock->getItemById($blockId);
			$form->setFormValues($dataDb);
			$form->getElement('item_content')->setValue($dataDb['content']);
		} else {
			$data = $this->_getAllParams();
            if ($form->isValid($data)) {
            	$data['content'] = $data['item_content'];
 		 		$this->_serviceBlock->updateItem($data, $blockId);
 		 		return $this->_redirect("/page/admin/block.list/id/{$pageId}/tab_id/{$data['lang_id']}" . Main_Const::getSeriveUrlStr());      		    			
            }
		}   
		
		$this->view->form = $form;
		return $this->render();
	}
	
	/**
	 * @return void
	 */
	public function blockAddAction() 
	{
		$pageId = $this->_getParam('page_id');
		$form = $this->_formAdminBlockEdit;
		$form->getElement('page_id')->setValue($pageId);
		
		if ($this->_request->isPost()) {
			$data = $this->_getAllParams();
            if ($form->isValid($data)) {
            	$data['content'] = $data['item_content'];
 		 		$this->_serviceBlock->insertItem($data);
 		 		return $this->_redirect("/page/admin/block.list/id/{$pageId}/tab_id/{$data['lang_id']}" . Main_Const::getSeriveUrlStr());
            }
		}    
		
		$this->view->form = $form;
		return $this->render();
	}
	
	/**
	 * @return json
	 */
	public function ajaxPagerBlockAction()
	{
	    if (!$this->_request->isXmlHttpRequest()) {
            $this->_response->setHttpResponseCode(404)->sendHeaders();
            return $this->_redirect('/error/error');
        }
        
        $type = $this->_getParam('type');
        $pageId = $this->_getParam('page_id');
     	$pager = Main_Pager::factory('Page_Pager_AdminBlock', self::PAGER_NAME_BLOCK . "_" . $type, array('type' => $type, 'page_id' => $pageId)); 
     	
     	$result = $pager->getResponse($_REQUEST);

     	$this->_helper->json($result);   
	}
	
	// --- block --- }}
	
	
}
